package com.crm.controller.after_sale;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.crm.pojo.ClientComplaint;
import com.crm.pojo.ClientInfo;
import com.crm.service.after_sale.ComplaintService;
import com.crm.service.client.ClientInfoService;
import com.crm.vo.Result;

@Controller
public class ComplaintController {

	@Autowired
	private ComplaintService complaintService;
	@Autowired
	private ClientInfoService clientInfoService;
	
	/**
	 * 客户投诉页面
	 * @return
	 */
	@GetMapping("/complaint")
	public String complaint() {
		return "after-sale/complaint";
	}
	
	/**
	 * 新增客户投诉页面
	 * @return
	 */
	@GetMapping("/complaint/to-add-complaint")
	public String toAddClientComplaint() {
		return "after-sale/add-complaint";
	}
	
	/**
	 * 编辑客户投诉页面
	 * @return
	 */
	@GetMapping("/complaint/to-edit-complaint")
	public String toEditClientComplaint() {
		return "after-sale/edit-complaint";
	}
	
	/**
	 * 客户投诉视图页面
	 * @return
	 */
	@GetMapping("/complaint/to-view-complaint")
	public String toViewClientComplaint() {
		return "after-sale/view-complaint";
	}
	
	/**
	 * 分页查询客户投诉
	 * @param page
	 * @param rows
	 * @return
	 */
	@PostMapping("/complaint/find-complaint-by-page")
	@ResponseBody
	public Map findComplaintByPage( int page, int rows){
		Long total = complaintService.countComplaintByPage();
		List<ClientComplaint> clientComplaints = complaintService.findComplaintByPage(page, rows);
		Map map = new HashMap<>();
		map.put("total", total);
		map.put("rows", clientComplaints);
		return map;
	}
	
	/**
	 * 新增客户投诉
	 * @param clientComplaint
	 * @return
	 */
	@PostMapping("/complaint/add-complaint")
	@ResponseBody
	public Result addClientComplaint(ClientComplaint clientComplaint) {
		return complaintService.addClientComplaint(clientComplaint);
	}
	
	
	/**
	 * 根据客户投诉编号逻辑删除客户投诉
	 * @param complaintId
	 * @return
	 */
	@PostMapping("/complaint/del-complaint")
	@ResponseBody
	public Result delClientComplaint(Long complaintId) {
		return complaintService.delClientComplaint(complaintId);
	}
	
	/**
	 * 编辑客户投诉
	 * @param clientComplaint
	 * @return
	 */
	@PostMapping("/complaint/edit-complaint")
	@ResponseBody
	public Result editClientComplaint(ClientComplaint clientComplaint) {
		return complaintService.editClientComplaint(clientComplaint);
	}
	
	/**
	 * 根据客户投诉编号查询客户投诉
	 * @param complaintId
	 * @param page
	 * @param rows
	 * @return
	 */
	@PostMapping("/complaint/find-complaint-by-complaintId")
	@ResponseBody
	public Map findComplaintBycomplaintId(Long complaintId, int page, int rows) {
		List<ClientComplaint> complaints = complaintService.findComplaintBycomplaintId(complaintId, page, rows);
		Map map = new HashMap<>();
		map.put("total", 1);
		map.put("rows", complaints);
		return map;
	}
	
	/**
	 * 通过投诉主题模糊分页查询客户投诉
	 * @param theme
	 * @param page
	 * @param rows
	 * @return
	 */
	@PostMapping("/complaint/find-complaint-by-theme")
	@ResponseBody
	public Map findByTheme(String theme,  int page, int rows) {
		Long total = complaintService.countByTheme(theme);
		List<ClientComplaint> clientComplaints = complaintService.findByTheme(theme, page, rows);
		Map map = new HashMap<>();
		map.put("total", total);
		map.put("rows", clientComplaints);
		return map;
	}
	
	
	@PostMapping("/complaint/find-clientInfo-cid")
	@ResponseBody
	public ClientInfo load(Long cid) {
		return clientInfoService.load(cid);
	}
}
